<?php session_start();
		  include('includes/header.php');
		  include("../includes/config.php"); 

		  $q2 = "SELECT count(id_image) as total from `background-images`";
		  $maxImgs = mysql_fetch_array(mysql_query($q2)) or die(mysql_error());

		  if(isset($_GET['deleteCity'])){
			mysql_query("delete from `background-images` where id_image='$_GET[deleteCity]'");
			
		  }
		  if($_POST['save']){
			 $i = 1;
			 $imgOrder = array();
			 $orders = array();	 
			 $imgOrder = $_POST;
			 $totalRows = count($imgOrder);
			 $checkArr = array_unique($imgOrder);
			  
			 if(count($checkArr) == $totalRows){
				reset ($_POST);
				 while (list ($key, $val) = each ($_POST)) {
						$q = "update `background-images` 
											  set img_order = '".$val."'
											  where id_image = ".$key;
											
						 mysql_query($q);
						 $i++;
				 }
				
			  }	else{
				$error = "<h2 style=\"color:red; margin:0;\"> geen unieke volgorde</h2>";
			  }			  
		  }
	
?>
		  
  <div  id="main" role="main" style="font-family:arial">
  	<?php if($error) echo $error;?>
		
  <form method="POST" action ="#">
	
	  <table style="border-collapse:separate; border-spacing:10px;">
			<tr><td><a style="list-style:none; color:#000; text-decoration:none; font-size:20px; padding:0px;" href="nieuweAchtergrond.php">Voeg nieuwe achtergrond toe <img src="http://cdn1.iconfinder.com/data/icons/prettyoffice/32/add1-.png"/> </a></td></tr>
		  <tr style="font-weight:bold"><td>Plaatje</td><td>Volgorde</td><td>stad</td><td>text</td><td>prijs</td><td>wijzig</td><td>verwijder</td></tr>
			<?php 
				$q = "SELECT * FROM `background-images` order by img_order";
				$q2 = "SELECT count(id_image) as total from `background-images`";
				$maxImg = mysql_fetch_array(mysql_query($q2)) or die(mysql_error());
				
				$res = mysql_query($q) or die(mysql_error());
				
				while($img = mysql_fetch_array($res)){
					$i = 1;
					echo "<tr><td><img src='".$img['url']."' height=\"100\" /></td>";
					echo "<td><select name=".$img['id_image'].">
							 <option>".$img['img_order']."</option>";
							while($i <= $maxImg['total']){
								if($img['img_order'] == $i){
									$i++;
								}else{
									echo "<option>".$i."</option>";
									$i++;
								}
							}
					echo "</select></td>";
					echo "<td>".$img['city']."</td>";
					echo "<td>".$img['text']."</td>";
					echo "<td>".$img['price']."</td>";
					echo '<td><a href="editAchtergrond.php?imageId='.$img['id_image'].'"><img src="http://cdn1.iconfinder.com/data/icons/diagona/icon/10/018.png"/></a></td>';
					echo '<td><a href="?deleteCity='.$img['id_image'].'"><img  src="http://cdn1.iconfinder.com/data/icons/diagona/icon/10/101.png"/></a></td></tr>';
					
				}
			?>
		</table>
		<input type="submit" name="save" value="Wijzig" />
	</form>
  </div>

  
  
<?php include('includes/footer.php');?>